﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Interactivity;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace JHMVVMFramework.Common.Behavior
{
    public class OrientationToStateBehavior : Behavior<UserControl>
    {
        public bool IsEnable { get; set; }

        [CustomPropertyValueEditor(CustomPropertyValueEditor.StateName)]
        public string PortraitUpStateName { get; set; }
        [CustomPropertyValueEditor(CustomPropertyValueEditor.StateName)]
        public string PortraitDownStateName { get; set; }

        [CustomPropertyValueEditor(CustomPropertyValueEditor.StateName)]
        public string LandscapeLeftStateName { get; set; }
        [CustomPropertyValueEditor(CustomPropertyValueEditor.StateName)]
        public string LandscapeRightStateName { get; set; }

        PhoneApplicationFrame m_mainFrame;

        public OrientationToStateBehavior()
        {
            IsEnable = true;
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            InitWhenReady();
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            if (m_mainFrame != null)
            {
                m_mainFrame.OrientationChanged -= main_OrientationChanged;
            }
        }

        private void InitWhenReady()
        {
            base.Dispatcher.BeginInvoke
                (
                    () =>
                    {
                        m_mainFrame = Application.Current.RootVisual as PhoneApplicationFrame;
                        if (m_mainFrame != null)
                        {
                            m_mainFrame.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(main_OrientationChanged);
                            SetState(m_mainFrame.Orientation);
                        }
                        else
                        { InitWhenReady(); }
                    }
                );
        }

        private void SetState(PageOrientation orientation)
        {
            string stateName = "";

            switch (orientation)
            {
                case PageOrientation.PortraitUp:
                    stateName = PortraitUpStateName;
                    break;
                case PageOrientation.PortraitDown:
                    stateName = PortraitDownStateName;
                    break;
                case PageOrientation.LandscapeLeft:
                    stateName = LandscapeLeftStateName;
                    break;
                case PageOrientation.LandscapeRight:
                    stateName = LandscapeRightStateName;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            if (stateName != null)
            {
                VisualStateManager.GoToState(base.AssociatedObject, stateName, true);
            }
        }

        void main_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if (IsEnable)
                SetState(e.Orientation);
        }

    }
}
